var buttonNormal : Texture2D;
var buttonHover : Texture2D;
var buttonClick : Texture2D;

//var splosion : GameObject;
//var location1 : GameObject;
//var location2 : GameObject;
//var location3 : GameObject;
//var location4 : GameObject;
//var location5 : GameObject;

var lemonCount : GameObject;
var appleCount : GameObject;
var blackberryCount : GameObject;
var blueberryCount : GameObject;
var peachCount : GameObject;
var strawberryCount : GameObject;
var targetPie : GameObject;
var mainCamera : GameObject;
var targetApples : GameObject;
var targetBlackberries : GameObject;
var targetBlueberries : GameObject;
var targetLemons : GameObject;
var targetPeaches : GameObject;
var targetStrawberries : GameObject;

var piesMade : int;

private var appleMatch : boolean;
private var blackberryMatch : boolean;
private var blueberryMatch : boolean;
private var lemonMatch : boolean;
private var peachMatch : boolean;
private var strawberryMatch : boolean;

private var appleMultiple : int;
private var blackberryMultiple : int;
private var blueberryMultiple : int;
private var lemonMultiple : int;
private var peachMultiple : int;
private var strawberryMultiple : int;

private var lemonScript : LemonsCount;
private var appleScript : ApplesCount;
private var blackberryScript : BlackberriesCount;
private var blueberryScript : BlueberriesCount;
private var peachScript : PeachesCount;
private var strawberryScript : StrawberriesCount;
private var clickScript : ClickScript;
private var targetScript : TargetPieScript;
private var targetAppleScript : TargetApples;
private var targetBlackberryScript : TargetBlackberries;
private var targetBlueberryScript : TargetBlueberries;
private var targetLemonScript : TargetLemons;
private var targetPeachScript : TargetPeaches;
private var targetStrawberryScript : TargetStrawberries;


function Start (){
	lemonScript = lemonCount.gameObject.GetComponent("LemonsCount");
	appleScript = appleCount.gameObject.GetComponent("ApplesCount");
	blackberryScript = blackberryCount.gameObject.GetComponent("BlackberriesCount");
	blueberryScript = blueberryCount.gameObject.GetComponent("BlueberriesCount");
	peachScript = peachCount.gameObject.GetComponent("PeachesCount");
	strawberryScript = strawberryCount.gameObject.GetComponent("StrawberriesCount");
	clickScript = mainCamera.gameObject.GetComponent("ClickScript");
	targetScript = targetPie.gameObject.GetComponent("TargetPieScript");
	targetAppleScript = targetApples.gameObject.GetComponent("TargetApples");
	targetBlackberryScript = targetBlackberries.gameObject.GetComponent("TargetBlackberries");
	targetBlueberryScript = targetBlueberries.gameObject.GetComponent("TargetBlueberries");
	targetLemonScript = targetLemons.gameObject.GetComponent("TargetLemons");
	targetPeachScript = targetPeaches.gameObject.GetComponent("TargetPeaches");
	targetStrawberryScript = targetStrawberries.gameObject.GetComponent("TargetStrawberries");

	
	
	appleMatch = false;
	blackberryMatch = false;
	blueberryMatch = false;
	lemonMatch = false;
	peachMatch = false;
	strawberryMatch = false;
	
	appleMultiple = 1;
	blackberryMultiple = 1;
	blueberryMultiple = 1;
	lemonMultiple = 1;
	peachMultiple = 1;
	strawberryMultiple = 1;
	
	piesMade = 0;
}


function OnMouseEnter()
{
	renderer.material.mainTexture = buttonHover;
	
}

function OnMouseExit()
{
	renderer.material.mainTexture = buttonNormal;
}

function OnMouseDown()
{
	renderer.material.mainTexture = buttonClick;
}


function OnMouseUp()
{
	yield BakePie();
	
	
	for (i = lemonScript.lemonCount; i > 0; i = i-1){
		lemonScript.Decrease();
		clickScript.DecreaseTotal();
	}
	for (i = appleScript.appleCount; i > 0; i = i-1){
		appleScript.Decrease();
		clickScript.DecreaseTotal();
	}
	for (i = blackberryScript.blackberryCount; i > 0; i = i-1){
		blackberryScript.Decrease();
		clickScript.DecreaseTotal();
	}
	for (i = blueberryScript.blueberryCount; i > 0; i = i-1){
		blueberryScript.Decrease();
		clickScript.DecreaseTotal();
	}
	for (i = peachScript.peachCount; i > 0; i = i-1){
		peachScript.Decrease();
		clickScript.DecreaseTotal();
	}
	for (i = strawberryScript.strawberryCount; i > 0; i = i-1){
		strawberryScript.Decrease();
		clickScript.DecreaseTotal();
	}
}

function BakePie() : IEnumerator
{
	total = clickScript.totalCount;
	apples = appleScript.appleCount;
	blackberries = blackberryScript.blackberryCount;
	blueberries = blueberryScript.blueberryCount;
	lemons = lemonScript.lemonCount;
	peaches = peachScript.peachCount;
	strawberries = strawberryScript.strawberryCount;
	
	
	appleMultiple = 1;
	blackberryMultiple = 1;
	blueberryMultiple = 1;
	lemonMultiple = 1;
	peachMultiple = 1;
	strawberryMultiple = 1;
	
	appleMatch = false;
	blackberryMatch = false;
	blueberryMatch = false;
	lemonMatch = false;
	peachMatch = false;
	strawberryMatch = false;
	
	//find lowest form of fraction
	for (var i = apples; i > 0; i = i - 1) {
		if ((apples % i == 0) && (total % i == 0)){
			//common factor found
			appleMultiple = i;
			
			i = 0;
		}
			
	}
	
	//find lowest form of fraction
	for (i = blackberries; i > 0; i = i - 1) {
		if ((blackberries % i == 0) && (total % i == 0)){
			//common factor found
			blackberryMultiple = i;
			i = 0;
		}
			
		
	}
	
	//find lowest form of fraction
	for (i = blueberries; i > 0; i = i - 1) {
		if ((blueberries % i == 0) && (total % i == 0)){
			//common factor found
			blueberryMultiple = i;
			i = 0;
		}
			
		
	}
	
	//find lowest form of fraction
	for (i = lemons; i > 0; i = i - 1) {
		if ((lemons % i == 0) && (total % i == 0)){
			//common factor found
			lemonMultiple = i;
			i = 0;
		}
			
		
	}
	
	//find lowest form of fraction
	for (i = peaches; i > 0; i = i - 1) {
		if ((peaches % i == 0) && (total % i == 0)){
			//common factor found
			peachMultiple = i;
			i = 0;
		}
			
		
	}
	
	//find lowest form of fraction
	for (i = strawberries; i > 0; i = i - 1) {
		if ((strawberries % i == 0) && (total % i == 0)){
			//common factor found
			strawberryMultiple = i;
			i = 0;
		}
			
		
	}
	
	Debug.Log("apples = " + apples);
	Debug.Log("apple mult = " + appleMultiple);
	Debug.Log("targetApples = " + targetAppleScript.applesToDisplay);
	
	//Debug.Log("blackberry mult = " + blackberryMultiple);
	//Debug.Log("blackberry = " + blackberries);
	//Debug.Log("targetblackberries = " + targetBlackberryScript.blackberriesToDisplay);
	
	Debug.Log("total = " + total);
	Debug.Log("apple targetTotal = " + targetAppleScript.totalToDisplay);
	//Debug.Log("blackberry targetTotal = " + targetBlackberryScript.totalToDisplay);
	
	if ( (apples/appleMultiple == targetAppleScript.applesToDisplay) 
		&& (total/appleMultiple == targetAppleScript.totalToDisplay) ) {
		//apples match correctly
		appleMatch = true;
		}
	if ( (blackberries/blackberryMultiple == targetBlackberryScript.blackberriesToDisplay) 
		&& (total/blackberryMultiple == targetBlackberryScript.totalToDisplay) ) {
		//blackberries match correctly
		blackberryMatch = true;
		}
	if ( (blueberries/blueberryMultiple == targetBlueberryScript.blueberriesToDisplay) 
		&& (total/blueberryMultiple == targetBlueberryScript.totalToDisplay) ) {
		//blueberries match correctly
		blueberryMatch = true;
		}
	if ( (lemons/lemonMultiple == targetLemonScript.lemonsToDisplay)
		&& (total/lemonMultiple == targetLemonScript.totalToDisplay) ) {
		//lemons match correctly
		lemonMatch = true;
		}
	if ( (peaches/peachMultiple == targetPeachScript.peachesToDisplay) 
		&& (total/peachMultiple == targetPeachScript.totalToDisplay) ) {
		//blueberries match correctly
		peachMatch = true;
		}
	if ( (strawberries/strawberryMultiple == targetStrawberryScript.strawberriesToDisplay)
		&& (total/strawberryMultiple == targetStrawberryScript.totalToDisplay) ) {
		//lemons match correctly
		strawberryMatch = true;
		}
	
	
	if ( appleMatch && blackberryMatch && blueberryMatch && lemonMatch && peachMatch && strawberryMatch) {
		Debug.Log ("correct");
		
		//var cloneBurst1 = Instantiate(splosion, location1.transform.position, splosion.transform.rotation);
		//var cloneBurst2 = Instantiate(splosion, location2.transform.position, splosion.transform.rotation);
		//var cloneBurst3 = Instantiate(splosion, location3.transform.position, splosion.transform.rotation);
		//var cloneBurst4 = Instantiate(splosion, location4.transform.position, splosion.transform.rotation);
		//var cloneBurst5 = Instantiate(splosion, location5.transform.position, splosion.transform.rotation);
		
		clickScript.score = clickScript.score + 50*total*appleMultiple;
		
		StaticVars.piesMade = piesMade;
		
		if ((piesMade == 4) && (StaticVars.mode == 1)){
			
			StaticVars.piesMade = 5;
			Application.LoadLevel(5);
			}
		else {
		piesMade = piesMade + 1;
		}
		
		
		targetScript.CreatePie();
		}
	else {
		if (clickScript.score < 1000) {
		clickScript.score = 0;
		}
		else{
		clickScript.score = clickScript.score - 1000;
		}
		Debug.Log ("wrong");
		}

}